#include "ResSprite.h"
#include "D3D9Engine.h"


CResourceSprite::CResourceSprite()
{
	_dxSprite = NULL;
	_spriteTex = NULL;

	_fileName = NULL;
	_width = _height = 0;
	_colorKey = D3DCOLOR_XRGB(0, 0, 0);


	_drawMode = DM_USEALPHA;
	_colorOp = false;
	_flip = SF_HORIZONTAL;

	_type = NODE_ENTITY_ELEMENT;
	_entity = ENTITY_SPRITE;


	this->SetColliding(true);
}


CResourceSprite::~CResourceSprite()
{

}


bool CResourceSprite::OnInitResource(ICompHandler *handler)
{
	HRESULT hr;

	CD3D9Engine* ptrEngine = (CD3D9Engine*)handler;

	if(ptrEngine)
	{
		hr = D3DXCreateSprite(ptrEngine->GetDevice(), &_dxSprite);

		if(FAILED(hr))
			return false;


		this->_spriteTex = ptrEngine->GetResourceFactory()->RegisterTexture(this->_fileName, &this->_width, &this->_height, this->_colorKey);

		if(this->_spriteTex != NULL)
		{
			SetRect(&this->_renderedSize, 0, 0, this->_width, this->_height);
		}
	}

	return false;
}


bool CResourceSprite::OnFreeResource(ICompHandler *handler)
{
	CD3D9Engine* ptrEngine = (CD3D9Engine*)handler;
	if(ptrEngine)
	{
		ptrEngine->GetResourceFactory()->UnregisterTexture(this->_fileName);
		this->_spriteTex = NULL;
	}

	//SAFE_RELEASE(_spriteTex);
	SAFE_RELEASE(_dxSprite);

	return true;
}


bool CResourceSprite::CreateSprite(const char *fileName, int width, int height, DWORD colorKey)
{
	_fileName = (char*)fileName;
	_width = width;
	_height = height;
	_colorKey = colorKey;

	this->_srcWidth = width;
	this->_srcHeight = height;
	this->RotCenter = D3DXVECTOR3((float)width / 2.0f, (float)height / 2.0f, 0.0f);

	return true;
}


void CResourceSprite::UpdateNode()
{
	this->AdvanceAnimation(this->DeltaTime, &_renderedSize);

}


void CResourceSprite::Render(LPDIRECT3DDEVICE9 rendererDevice, D3DXMATRIX* transform)
{
	if(rendererDevice)
	{
		if(_dxSprite)
		{
			_dxSprite->Begin(D3DXSPRITE_ALPHABLEND);


			switch(_drawMode)
			{
			case DM_USEALPHA:
				{
					rendererDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
				}
				break;
			case DM_NOALPHA:
				{
					rendererDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
					rendererDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
				}
				break;
			case DM_LIGHTMAP:
				{
					rendererDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
					rendererDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
					rendererDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
					rendererDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
				}
				break;
			}


			if(_colorOp)
			{
				rendererDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
				rendererDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
				rendererDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
			}


			D3DXMATRIX* matTransform = ((transform != NULL) ? (transform) : (&this->WorldTransformation));

			_onScreenRect.left = (LONG)matTransform->_41;
			_onScreenRect.top = (LONG)matTransform->_42;

			_dxSprite->SetTransform(matTransform);	

			_dxSprite->Draw(_spriteTex, &_renderedSize, NULL, NULL, Color);

			_dxSprite->End();

			_onScreenRect.right = _onScreenRect.left + (_renderedSize.right - _renderedSize.left);
			_onScreenRect.bottom = _onScreenRect.top + (_renderedSize.bottom - _renderedSize.top);
		}
	}
}